<?php
set_time_limit(0);
include("../config.php");

// This file finds all images for an artist and caches them as binary in the database so that
// they can be retrieved without making an API call to beat rate limits
// You would probably want to build this into your code as you go, rather than cache images all in one
// Created 28th November 2012
// dan.brown180@emimusic.com

echo "<select name = \"input_artist\" id = \"input_artist\" onchange=\"window.location='cache_images.php?input_artist='+this.value;\">
";
echo "<option value = \"\" selected=selected>Select an artist...</option>";
$result = mysql_query("SELECT DISTINCT(`artist_name`) FROM `$sandbox_key` ORDER BY `artist_name` ASC") or die(mysql_error());  
while($row = mysql_fetch_array($result))
{
$artist_name = $row['artist_name'];
if ($artist_name != "")
{
echo "<option value = \"$artist_name\">$artist_name</option>
";
}
}
echo "</select><Br>
";

$input_artist=$_GET['input_artist'];
$result2 = mysql_query("SELECT * FROM `$sandbox_key` WHERE `artist_name` = \"$input_artist\" && `type` = 'release_image' LIMIT 25") or die(mysql_error());  
while($row2 = mysql_fetch_array($result2)) 
{
$image_id = $row2['file_asset_id'];
$image_src = $sandboxApi->access($image_id);
$image_src = $image_src['assets'][0]['url'];
$asset_cache = $row2['asset_cache'];
if ($asset_cache == "")
{
// Here is some code to cache the image in the database
$image_cache=file_get_contents($image_src);
$image_cache = addslashes($image_cache);
mysql_query("UPDATE `$sandbox_key` SET `asset_cache` = \"$image_cache\" WHERE `file_asset_id` = \"$image_id\"") or die(mysql_error());
}
}


// View images that have been cached
$result2 = mysql_query("SELECT * FROM `$sandbox_key` WHERE `artist_name` = \"$input_artist\" && `type` = 'release_image' LIMIT 25") or die(mysql_error());  
while($row2 = mysql_fetch_array($result2)) 
{
$image_cache = $row2['asset_cache'];
echo '<img src="data:image/jpeg;base64,' . base64_encode($image_cache) . '" />';
}

?>